﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SBLib.Common.CsvKS
{
    public class DataTableWriter : CsvBase
    {
        string filename = string.Empty;
        string delimiter = string.Empty;
        DataTable dt;

        public DataTableWriter(string FileName, DataTable dt, string Delimiter)
            : base(FileName, Delimiter)
        {
            filename = FileName;
            this.dt = dt;
            delimiter = Delimiter;
        }

        protected override void ProcessObjectForWriting()
        {
            // Header
            string[] colLbls = new string[dt.Columns.Count];

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                colLbls[i] = dt.Columns[i].ColumnName;
                colLbls[i] = GetWriteableValueForCsv(colLbls[i]);
            }

            // Write labels
            _swFile.WriteLine(string.Join(delimiter, colLbls));

            // Rows of Data
            foreach (DataRow rowData in dt.Rows)
            {
                string[] colData = new string[dt.Columns.Count];

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    object obj = rowData[i];
                    colData[i] = GetWriteableValueForCsv(obj);
                }

                // Write data in row
                _swFile.WriteLine(string.Join(delimiter, colData));
            }
        }


        protected override void ProcessObjectForWriting<T>(List<T> CustomObjectCollection)
        {

        }
    }
}
